Skip to content

Conversation

svozza
Copy link
Contributor

@svozza svozza commented Sep 5, 2025

Summary

This PR implements route-specific middleware functionality for the REST event handler, allowing middleware to be applied to individual routes in addition to global middleware. E.g.,

// functional
const middleware1 = async (params, options, next) => {
    console.log('middleware1-start');
    await next();
    console.log('middleware1-end');
};

app.get('/test', [middleware1], (params, {event, context, request}) => {
  return { result: 'get-decorator-middleware' };
});

// decorators
@app.get('/test', [middleware1])
public async getTest() {
    return { result: 'get-decorator-middleware' };
}

Changes

Route Class (packages/event-handler/src/rest/Route.ts)

  • Added middleware property to Route class constructor with default empty array

BaseRouter (packages/event-handler/src/rest/BaseRouter.ts)

  • Updated all HTTP method signatures to support optional middleware parameter: get(path, middleware?, handler)
  • Added overloads for decorator syntax with middleware.
  • Modified resolve() method to compose global and route-specific middleware
  • Route-specific middleware executes after global middleware in execution chain

RouteHandlerRegistry (packages/event-handler/src/rest/RouteHandlerRegistry.ts)

  • Updated resolve() method to include middleware in returned RouteHandlerOptions

Tests

  • Created factory functions for creating middlewares to reduce code duplication
  • Tests for functional middleware
  • Decorator tests

Issue number: closes #4430


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@svozza svozza requested a review from dreamorosi September 5, 2025 16:19
@svozza svozza self-assigned this Sep 5, 2025
@pull-request-size pull-request-size bot added the size/XL PRs between 500-999 LOC, often PRs that grown with feedback label Sep 5, 2025
@boring-cyborg boring-cyborg bot added event-handler This item relates to the Event Handler Utility tests PRs that add or change tests labels Sep 5, 2025
Copy link
Contributor

@dreamorosi dreamorosi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a few comments / questions - the implementation is quite involved but looks ok.

Fell free to request another review when ready.

@svozza svozza force-pushed the event-hanlder/route-level-middleware branch from 4a96a5e to 4be1dce Compare September 5, 2025 18:39
@svozza svozza requested a review from dreamorosi September 5, 2025 18:39
Copy link

sonarqubecloud bot commented Sep 8, 2025

Copy link
Contributor

@dreamorosi dreamorosi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ok to merge this one as is, including the complexity warning from Sonar since it'll be removed once #4438 is addressed.

@svozza svozza merged commit e6ea674 into main Sep 8, 2025
37 checks passed
@svozza svozza deleted the event-hanlder/route-level-middleware branch September 8, 2025 11:19
@svozza svozza added this to the Event Handler Beta (priority) milestone Sep 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

event-handler This item relates to the Event Handler Utility size/XL PRs between 500-999 LOC, often PRs that grown with feedback tests PRs that add or change tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: Add Route Specific Middleware for REST Event Hndler

2 participants